package module.ahleteView;

import bean.Athlete;
import bean.Competition;
import bean.Constant;
import bean.EntryForm;
import dao.impl.AthleteDaoImpl;
import dao.impl.CompetitionDaoImpl;
import dao.impl.EntryFormDaoImpl;
import sun.swing.table.DefaultTableCellHeaderRenderer;

import java.util.*;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Auther leiHua
 * @Date 2022/5/14 14:25
 */
public class AthleteBaoming {
    //创建的表格宽度
    int tableWidth = 1000;
    //创建的表格高度
    int tableHeight = 650;
    JTable jTable;
    JButton baoming;
    JButton certain;

    /**
     * 创建包含所有的比赛的表格
     * Jpanel->JscrollPane->Jtable
     *
     * @return Jpanel
     */
    public JPanel creatCompetitionTable() {
        JPanel jPanel = new JPanel();  //最大容器
        jPanel.setSize(1000, 1000);
        jPanel.setLayout(null);

        JScrollPane jScrollPane = new JScrollPane(); //滚动窗口
        jScrollPane.setSize(tableWidth, tableHeight);
        jScrollPane.setBounds(0, 0, tableWidth, tableHeight);

        jTable = new JTable(0, 5);

        jTable.setFont(new Font("微软雅黑", Font.TYPE1_FONT, 18));  //设置表格字体
        //设置行高
        jTable.setRowHeight(30);
        //设置列名
        Object[] header = {"id", "比赛名称", "比赛地点", "最多参赛人数", "比赛时间"};
        DefaultTableModel model = (DefaultTableModel) jTable.getModel();
        model.setColumnIdentifiers(header); //设置列标识符 （列名）
        //列名字体设置
        jTable.getTableHeader().setFont(new Font("宋体", Font.TYPE1_FONT, 24));

        jTable.getTableHeader().setBackground(new Color(211, 237, 215));
//        model.addRow(header);
//-----------------------------------------------------------------------------------------
        //查询competiton表中信息
        CompetitionDaoImpl competitionDao = new CompetitionDaoImpl();
        ArrayList<Competition> arrayList = competitionDao.selectAll();
        for (Competition competition : arrayList) {
            String c_id = competition.getC_id();
            String competitionName = competition.getCompetitionName();
            String palce = competition.getPalce();
            int number = competition.getNumber();
            String time = competition.getTime();

            Vector vector = new Vector();
            vector.add(c_id);
            vector.add(competitionName);
            vector.add(palce);
            vector.add(number);
            vector.add(time);
            model.addRow(vector);
        }

        //设置文字居中显示(这段是网上copy找的)
        DefaultTableCellRenderer dc = new DefaultTableCellRenderer();
        dc.setHorizontalAlignment(JLabel.CENTER);
        jTable.setDefaultRenderer(Object.class, dc);
        //设置表头文字居中
        DefaultTableCellHeaderRenderer hr = new DefaultTableCellHeaderRenderer();
        hr.setHorizontalAlignment(JLabel.CENTER);
        jTable.getTableHeader().setDefaultRenderer(hr);

        jTable.setModel(model);
        //将视图设为表格setViewportView
        //jScrollPane.add(jTable); (不行)
        jScrollPane.setViewportView(jTable);
        jPanel.add(jScrollPane);
        jScrollPane.setAutoscrolls(true);
        baoming = new JButton("报名");
        baoming.setFont(new Font("宋体", Font.BOLD, 24));
        baoming.setBounds(400, 650, 100, 40);
        baoming.addActionListener(new BaomingAction());
        jPanel.add(baoming);
        return jPanel;
    }

    /**
     *
     */
    class BaomingAction implements ActionListener {
        //点击报名
        @Override
        public void actionPerformed(ActionEvent e) {
            int selectedRow = jTable.getSelectedRow();
            String action = e.getActionCommand();
            String competitionId = (String) jTable.getValueAt(selectedRow, 0);  //获取该项目的id
            //判断是否已经过名了
            EntryFormDaoImpl entryFromDao = new EntryFormDaoImpl();
            ArrayList<EntryForm> entryForms = entryFromDao.selectAll();
            boolean isExist = false;
            for (EntryForm entryForm : entryForms) {
                String c_id = entryForm.getCompetition().getC_id();
                String a_id = entryForm.getAthlete().getA_id();
                if (c_id.equals(competitionId) && a_id.equals(Athlete.myA_id)) {
                    isExist = true;
                    break;
                }
            }

            if (action.equals(baoming.getText())) {
                //DefaultTableModel tableModel=(DefaultTableModel) jTable.getModel();
                if (selectedRow == -1) {   //没有选择项目 提示用户进行选择
                    JLabel jLabel = new JLabel("请选择项目");
                    jLabel.setFont(new Font("宋体", Font.BOLD, 30));
                    JOptionPane.showMessageDialog(null, jLabel, "错误", JOptionPane.ERROR_MESSAGE);
                } else if (isExist) {
                    //判断该项目是否已经过名了
                    JLabel label = new JLabel("您已经报过该项目");
                    label.setFont(new Font("宋体", Font.BOLD, 30));
                    JOptionPane.showMessageDialog(null, label, "重复报名", JOptionPane.ERROR_MESSAGE);
                } else {
                    //获取到选择的行，创建新的窗口进行报名
                    System.out.println(selectedRow);
                    System.out.println(jTable.getValueAt(selectedRow, 0));
                    CompetitionDaoImpl competitionDao = new CompetitionDaoImpl();
                    Competition competition = competitionDao.select(competitionId);


                    //创建报名窗口
                    JFrame jFrame = new JFrame("运动会报名");
                    Dialog dialog = new Dialog(jFrame, true);

                    jFrame.setBounds((Constant.SCREEN_WIDETH - 600) / 2, (Constant.SCREEN_HEIGHT - 400) / 2, 800, 600);
                    jFrame.setVisible(true);
                    jFrame.setLayout(null);
//                    JLabel[] jLabels = new JLabel[5];
//                    String[] labelStr = {"项目名称", "姓名", "性别", "年龄", "系别"};

                    //---------------------------------------界面部分------------------------------------------
                    Font font = new Font("宋体", Font.BOLD, 24);
                    JLabel labelComName = new JLabel("项目名称:" + competition.getCompetitionName());
                    JLabel labelName = new JLabel("姓名:");
                    JLabel labelSex = new JLabel("性别:");
                    JLabel labelAge = new JLabel("年龄:");
                    JLabel labelDepartment = new JLabel("系别:");
                    labelAge.setFont(font);
                    labelComName.setFont(new Font("宋体", Font.BOLD, 30));
                    labelName.setFont(font);
                    labelDepartment.setFont(font);
                    labelSex.setFont(font);
                    labelComName.setBounds(150, 50, 600, 30);
                    labelName.setBounds(150, 100, 200, 30);
                    labelSex.setBounds(150, 150, 200, 30);
                    labelAge.setBounds(150, 200, 200, 30);
                    labelDepartment.setBounds(150, 250, 200, 30);

                    JTextField jTextField1 = new JTextField(18);
                    JTextField jTextField2 = new JTextField(18);
                    JTextField jTextField3 = new JTextField(18);
                    JTextField jTextField4 = new JTextField(18);
                    // JTextField jTextField5 = new JTextField(18);
                    jTextField1.setBounds(300, 100, 200, 40);
                    jTextField2.setBounds(300, 150, 200, 40);
                    jTextField3.setBounds(300, 200, 200, 40);
                    jTextField4.setBounds(300, 250, 200, 40);
                    jTextField1.setFont(font);
                    jTextField2.setFont(font);
                    jTextField3.setFont(font);
                    jTextField4.setFont(font);

                    jFrame.add(labelComName);
                    jFrame.add(labelName);
                    jFrame.add(jTextField1);
                    jFrame.add(labelSex);
                    jFrame.add(jTextField2);
                    jFrame.add(labelAge);
                    jFrame.add(jTextField3);
                    jFrame.add(labelDepartment);
                    jFrame.add(jTextField4);

                    jTextField1.setText(Athlete.myName);
                    jTextField2.setText(Athlete.mySex);
                    jTextField3.setText(String.valueOf(Athlete.myAge));
                    jTextField4.setText(Athlete.myDepartment);
                    certain = new JButton("确定");
                    jFrame.add(certain);
                    certain.setBounds(350, 350, 100, 60);
                    certain.setFont(font);
//----------↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑--  界面部分    ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑---------------------------------  ↑↑↑↑↑↑↑↑
                    //确定按钮
                    certain.addActionListener(new ActionListener() {
                        // 1、获取文本框内容
                        // 2、将文本框运动员信息 更新到数据库
                        // 3、判断该项目是否已经报过名了    报过名： 弹出消息框    未报名： 想数据库中写入
                        // 4、将该表名信息写入 报名信息表
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            String name = jTextField1.getText();
                            String sex = jTextField2.getText();

                            int age = 0;
                            try {  //限制年龄框只能输入数字
                                age = Integer.valueOf(jTextField3.getText());
                                String department = jTextField4.getText();
                                String myA_id = Athlete.myA_id;
                                Athlete athlete = new Athlete(myA_id, name, sex, age, department, Athlete.myC_id);
                                AthleteDaoImpl athleteDao = new AthleteDaoImpl();
                                athleteDao.update(athlete);
                                String uuid = UUID.randomUUID().toString();
                                entryFromDao.add(uuid, competitionId, myA_id, Constant.STATUS_CHECK);
                                //更新当前程序中运行内存用户的信息
                                Athlete.myAge=age;
                                Athlete.myDepartment=department;
                                Athlete.mySex=sex;
                                JLabel label = new JLabel("报名成功");
                                label.setFont(new Font("宋体", Font.BOLD, 30));
                                JOptionPane.showMessageDialog(null, label, "成功", JOptionPane.OK_OPTION);


                                jFrame.setVisible(false);
                            } catch (NumberFormatException ex) {
                                JLabel label = new JLabel("年龄框只能输入数字");
                                label.setFont(new Font("宋体", Font.BOLD, 30));
                                JOptionPane.showMessageDialog(null, label, "年龄框出错", JOptionPane.ERROR_MESSAGE);
                                ex.printStackTrace();
                            }

                        }

                    });
                }


                //jTable.setModel(tableModel);
            }

        }
    }

    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setLayout(null);

        AthleteBaoming talbe = new AthleteBaoming();
        JPanel jPanel = talbe.creatCompetitionTable();
        jPanel.setBounds(0, 0, jPanel.getWidth(), jPanel.getHeight());
        JPanel j2 = new JPanel();
        j2.setLayout(null);

        j2.add(jPanel);
        j2.setBounds(0, 0, 950, 1000);
        jFrame.add(j2);
        jFrame.setSize(1000, 1000);
        jFrame.setVisible(true);


    }
}
